package com.javacrimefighter.gwt.activemq.client;

import com.google.gwt.core.client.JavaScriptObject;

public final class ActiveMQ extends JavaScriptObject {

    protected ActiveMQ() {
    }
    
    public static native ActiveMQ get() /*-{
        return $wnd.amq
    }-*/;

    public native void initialize(String uri) /*-{
        this.uri = uri
    }-*/;
    
    
    public MessageHandlerRegistration subscribe(String destination, MessageHandler<?> handler) {
        final String id = nextId();
        doSubscribe(id,destination, handler);
        return new MessageHandlerRegistration() {
            public void removeHandler() {
                doUnsubscribe(id);
            }
        };
    }
    
    private native String nextId() /*-{
        if (!this.jcfNextId) {
            this.jcfNextId = 0
        }
        this.jcfNextId++
        return '' + this.jcfNextId
    }-*/;

    public native void send(String destination, String message) /*-{
        this.sendMessage(destination,message) 
    }-*/;

    private native void doSubscribe(String id, String destination, MessageHandler<?> handler) /*-{
        var callback = {
            receive: function(message) {
                handler.@com.javacrimefighter.gwt.activemq.client.MessageHandler::onMessage(Lcom/google/gwt/core/client/JavaScriptObject;)(message);
            }
        };
        this.addListener(id,destination,callback.receive);
    }-*/;

    
    private native void doUnsubscribe(String id) /*-{
        this.removeHandler(id);
    }-*/;

}
